#include "Lcd_AML015456.h"
#include "display_manager.h"
#include "bsp_lcd.h"
#include "nrf_delay.h"

int8_t lcd_aml015456_init(uint8_t mode)
{
    TFT_LCD_SDA_OUTPUT();
//    TFT_LCD_WR_OUTPUT();
    TFT_LCD_SCL_OUTPUT();
    TFT_LCD_CS_OUTPUT();
    TFT_LCD_RST_OUTPUT();
    TFT_LCD_EN_OUTPUT();

    TFT_LCD_SDA_HIGH();
//    TFT_LCD_WR_HIGH();
    TFT_LCD_SCL_LOW();
    TFT_LCD_SCL_HIGH();
    TFT_LCD_CS_HIGH();
    TFT_LCD_EN_HIGH();

    /*AML015456屏模式配置:1.54IM3~IM0:0101 3-line 9bit serial I/F*/

    nrf_delay_ms(1000);

    TFT_LCD_EN_HIGH();      // ????
    TFT_LCD_RST_HIGH(); 
    nrf_delay_ms(1);
    TFT_LCD_RST_LOW(); 
    nrf_delay_ms(150);      //Delay 100ms 
    TFT_LCD_RST_HIGH(); 
    nrf_delay_ms(150);      //Delay 120ms 
    TFT_LCD_CS_HIGH();
    TFT_LCD_SCL_HIGH();
    TFT_LCD_SDA_HIGH();

    Lcd_WriteIndex2(0x11);     

    nrf_delay_ms(120); //ms

    Lcd_WriteIndex2(0x36);     
    Lcd_WriteData2(0x00);   

    Lcd_WriteIndex2(0x3A);
    if(LCD_DISPLAY_RGB_MODE == mode)
    {
        Lcd_WriteData2(0x04);
    }
    else
    {
        Lcd_WriteData2(0x55);
    }

    if(LCD_DISPLAY_RGB_MODE == mode)
    {
        Lcd_WriteIndex2(0xB0);     
        Lcd_WriteData2(0x11);   
        Lcd_WriteData2(0xf0);   

        Lcd_WriteIndex2(0xB1);
        Lcd_WriteData2(0xc0);   
        Lcd_WriteData2(0x02);   
        Lcd_WriteData2(0x14); 
    }

    Lcd_WriteIndex2(0xB2);     
    Lcd_WriteData2(0x0C);   
    Lcd_WriteData2(0x0C);   
    Lcd_WriteData2(0x00);   
    Lcd_WriteData2(0x33);   
    Lcd_WriteData2(0x33);    

    Lcd_WriteIndex2(0xBB);   //VCOM  
    Lcd_WriteData2(0x1A);   

    Lcd_WriteIndex2(0xC0);     
    Lcd_WriteData2(0x2C);   

    Lcd_WriteIndex2(0xC2);     
    Lcd_WriteData2(0x01);   

    Lcd_WriteIndex2(0xC3);   //GVDD  
    Lcd_WriteData2(0x0F); //4.3V   

    Lcd_WriteIndex2(0xC4);     
    Lcd_WriteData2(0x20);   

    Lcd_WriteIndex2(0xC6);     
    Lcd_WriteData2(0x0F); //Dot INV, 60Hz  

    Lcd_WriteIndex2(0xD0);     
    Lcd_WriteData2(0xA4);   
    Lcd_WriteData2(0xA1);   

    Lcd_WriteIndex2(0xE0);     
    Lcd_WriteData2(0xD0);   
    Lcd_WriteData2(0x13);   
    Lcd_WriteData2(0x1A);   
    Lcd_WriteData2(0x0A);   
    Lcd_WriteData2(0x0A);   
    Lcd_WriteData2(0x26);   
    Lcd_WriteData2(0x3F);   
    Lcd_WriteData2(0x54);   
    Lcd_WriteData2(0x54);   
    Lcd_WriteData2(0x18);   
    Lcd_WriteData2(0x14);   
    Lcd_WriteData2(0x14);   
    Lcd_WriteData2(0x30);   
    Lcd_WriteData2(0x33);   

    Lcd_WriteIndex2(0xE1);     
    Lcd_WriteData2(0xD0);   
    Lcd_WriteData2(0x13);   
    Lcd_WriteData2(0x1A);   
    Lcd_WriteData2(0x0A);   
    Lcd_WriteData2(0x0A);   
    Lcd_WriteData2(0x26);   
    Lcd_WriteData2(0x3F);   
    Lcd_WriteData2(0x54);   
    Lcd_WriteData2(0x54);   
    Lcd_WriteData2(0x1A);   
    Lcd_WriteData2(0x16);   
    Lcd_WriteData2(0x16);   
    Lcd_WriteData2(0x32);   
    Lcd_WriteData2(0x35);    

    Lcd_WriteIndex2(0x29);   

    nrf_delay_ms(120); //ms

    Lcd_WriteIndex2(0x21);
}


int8_t lcd_aml015456_clear(uint16_t Color)
{
    uint16_t i,j;
    Lcd_SetRegion(0,0,240-1,240-1);
    for(i=0;i<240;i++)
    {
        for (j=0;j<240;j++)
        {
            LCD_WriteData_16Bit(Color);
        }
    }
    nrf_delay_ms(1800);
    Lcd_WriteIndex2(0x29);
}
